//
//  strategyViewController.m
//  HappilyLoan
//
//  Created by 纵情向前 on 2017/4/21.
//  Copyright © 2017年 zongqingxiangqian. All rights reserved.
//

#import "strategyViewController.h"
#import "MJRefresh.h"
#import "PublicClass.h"
#import "AlertBoxView.h"
#import "strategyView.h"
#import "strategyTableViewCell.h"
#import "TopStrategyModel.h"
#import "StrategyModel.h"
#import "UIImageView+WebCache.h"
@interface strategyViewController ()<UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UITextFieldDelegate>
@property(nonatomic,strong)UICollectionView *collectionView;
//@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong) strategyView *TopV;
@property(nonatomic,strong)AlertBoxView *alertBox;
@property (nonatomic, strong)TopStrategyModel *model;
@property(nonatomic,strong)NSMutableArray *dataArray;//数据
@property (nonatomic,assign) NSInteger pageNum;
@end

@implementation strategyViewController
#pragma mark -- getter/setter
- (AlertBoxView *)alertBox
{
    if (!_alertBox) {
        _alertBox = [[AlertBoxView alloc] init];
    }
    return _alertBox;
}



-(strategyView *)TopV{
    if (!_TopV) {
        _TopV = [[strategyView alloc]init];
    }
    return _TopV;
}

- (void)viewWillAppear:(BOOL)animated{


}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = GetColor(246, 247, 248, 1);
    self.automaticallyAdjustsScrollViewInsets = NO;//关闭偏移量
//    [self.view addSubview:self.tableView];
    [self.view addSubview:self.collectionView];
    //初始化Model，进行网络请求
    self.model = [[TopStrategyModel alloc] init];
    [self.model getStrategyWithPage:1];
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_StrategyNotification:) name:@"getStrategyModel" object:nil];
    
    
    // Do any additional setup after loading the view from its nib.
}
- (void)action_StrategyNotification:(NSNotification *)sender {
    //数据处理
    NSLog(@"攻略的数据：%@",sender.object);
    if (sender.object[@"data"][@"list"] != nil) {
        NSArray *array = sender.object[@"data"][@"list"];
    NSMutableArray *infoArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        StrategyModel *cellInfo = [StrategyModel getStrategyInfoDictionary:dic];
        [infoArray addObject:cellInfo];
    }
    self.dataArray = infoArray;
    }
    
    
    
//    _dataArray = [NSMutableArray array];
//    if ([sender.object[@"is_alert"] integerValue] == 1) {
//    }
//    
//    if ([sender.object[@"code"] integerValue] == 1) {
//        
//        for (NSDictionary *dic in sender.object[@"data"][@"list"]) {
//            StrategyModel *strInfo = [StrategyModel getStrategyInfoDictionary:dic];
//            NSLog(@"info:%@",strInfo);
//            [_dataArray addObject:strInfo];
//        }
//        
//    }
    
//        NSLog(@"攻略：HHH%@",_dataArray);

    
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];//
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;//定义纵向滑动
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        strategyView *TopV = [[strategyView alloc]init];
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.headerReferenceSize = TopV.topView.bounds.size;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 114) collectionViewLayout:layout];
        _collectionView.backgroundColor = GetColor(246, 247, 248, 1);
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerClass:[strategyTableViewCell class] forCellWithReuseIdentifier:@"cell"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectionHead"];
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(action_Reload)];
        UIView *centerView = [[UIView alloc] init];
        centerView.frame = CGRectMake(0, ADAPTATION_HEIGHT(413), MAINSCREEN_WIDTH, ADAPTATION_HEIGHT(30));
        centerView.backgroundColor = [UIColor whiteColor];
        [_collectionView addSubview:centerView];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.frame = CGRectMake(ADAPTATION_WIDTH(10), ADAPTATION_HEIGHT(5), ADAPTATION_WIDTH(70), ADAPTATION_HEIGHT(14));
        titleLabel.text = @"最新攻略";
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentRight;
        titleLabel.font = FONT(14);
        titleLabel.textColor = [UIColor blackColor];
        [centerView addSubview:titleLabel];
        
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(10/W, 29/H, (375-20)/W, 1);
        label.backgroundColor = [UIColor lightGrayColor];
        [centerView addSubview:label];
    }
    return _collectionView;
}
#pragma mark -- delegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;//每一排的个数

//    return 10;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;//几个分组
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    strategyTableViewCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];//重用
    StrategyModel *model = _dataArray[indexPath.row];
    NSLog(@"的个数：%@",self.dataArray);
    cell.backgroundColor = [UIColor whiteColor];
    cell.timeLabel.text = model.create_time;;
    cell.artLabel.text = model.activity_title;
    NSURL *url = [NSURL URLWithString:model.activity_pic];
    [cell.Image sd_setImageWithURL:url];
    cell.titleLabel.text = model.activity_content;

    //Cell图片
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(10/W, 59/H, (375-20)/W, 1);
    label.backgroundColor = [UIColor lightGrayColor];
    [cell addSubview:label];
    
    return cell;
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(MAINSCREEN_WIDTH, ADAPTATION_HEIGHT(60));
}
//设置当前分组之间的间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(ADAPTATION_HEIGHT(30), ADAPTATION_WIDTH(5), ADAPTATION_HEIGHT(5), ADAPTATION_WIDTH(5));
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    [self.TopV.topView removeFromSuperview];
    self.TopV.homeViewController = self;
    UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"collectionHead" forIndexPath:indexPath];
    headView.backgroundColor = [UIColor whiteColor];
    [headView addSubview:self.TopV.topView];
    return headView;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"你点我干嘛");
}

#pragma mark - 刷新
- (void)action_Reload {
    //刷新
    [self.collectionView.mj_header beginRefreshing];
//    [[MBLoadHUD getInitWithMB] start];
    [self performSelector:@selector(endRefresh) withObject:nil afterDelay:1.1];
}
- (void)endRefresh {
//    [[MBLoadHUD getInitWithMB] stop];
    [self.alertBox appearAndDisappearAlertWithString:@"暂无更多数据！"];
    [self.collectionView.mj_header endRefreshing];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
